package com.example.controller;

import com.example.result.Result;
import com.example.service.DictService;
import com.example.ylgh.model.cmn.Dict;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author hx
 * @create 2022/5/9 23:07
 * description:
 */
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    private DictService dictService;

    //省市联动接口
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
       List<Dict> dictList= dictService.findByDictCode(dictCode);
       return Result.ok(dictList);

    }

    //根据id查询子节点
    @GetMapping("/findChildrenData/{id}")
    public Result findChildrenData(@PathVariable Long id){
        List<Dict> list= dictService.findChildrenData(id);
        return Result.ok(list);
    }

    //导出数据字典接口
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportData(response);
    }

    //导入数据字典文件
    @PostMapping("/importData")
    public Result importData(MultipartFile file){
        dictService.importData(file);
        return Result.ok();
    }

    //根据dictcode和value查询
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value){
         return dictService.getDictName(dictCode,value);

    }
    //根据value查询
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value){
        return dictService.getDictName("",value);

    }
}
